﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class CostManage_CellMeter : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Attribute_Bind();
            Dep_Bind();
            Auth();
            SetPager();
            SetUnPager();
        }
    }

    private void Attribute_Bind()
    {
        Read_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('Read_Date')});");
        Pay_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('Pay_Date')});");
        Last_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('Last_Date')});");
        Last_Pay.Attributes.Add("onclick", "WdatePicker({el:$dp.$('Last_Pay')});");
    }

    private void Dep_Bind()
    {
        string sqlstr = "SELECT DISTINCT SUBSTR(Replace(CELL_CHNAME,'G',''),0,2) AS County FROM CELL_CLASS ORDER BY SUBSTR(Replace(CELL_CHNAME,'G',''),0,2)";
        Dep_A.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null).Tables[0].DefaultView;
        Dep_A.DataTextField = "County";
        Dep_A.DataValueField = "County";
        Dep_A.DataBind();
        Dep_A.Items.Insert(0, new ListItem("全市", "0"));
    }

    private void Auth()
    {
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            Dep_A.SelectedValue = dr["company"].ToString();
        }
        else
        {
            Dep_A.Enabled = true;
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetLocation();
        LocationPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = LocationPager.PageSize;
        pds.CurrentPageIndex = LocationPager.CurrentPageIndex - 1;
        LocationList.DataSource = pds;
        LocationList.DataBind();
    }

    private void SetUnPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetApproval();
        UnApplPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = UnApplPager.PageSize;
        pds.CurrentPageIndex = UnApplPager.CurrentPageIndex - 1;
        UnCellList.DataSource = pds;
        UnCellList.DataBind();
    }

    private DataSet GetApproval()
    {
        string sqlStr = "select * from U_METER_PAY_T where inputer = :inputer ";
        if (Dep_A.SelectedValue != "0")
        {
            sqlStr += " and Organ = '" + Dep_A.SelectedValue + "'";
        }
        if (Cell_CHName_A.Text.Trim().Length > 0)
        {
            sqlStr += " and PHYSICAL_NAME like '%" + Tools.ReplaceString(Cell_CHName_A.Text.Trim(), 50) + "%'";
        }
        OracleParameter[] _params = {
                    new OracleParameter(":inputer",OracleType.VarChar,16)
                };
        _params[0].Value = LoginUser;
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params);
    }

    private DataSet GetLocation()
    {
        string sqlStr = @"select A.PHYSICAL_NAME,Count(B.Cell_Chname) AS Cell_Count,A.CELLPOWER,A.METERWAY,A.BILLING_WAY,A.AMMETERNO 
                              from U_PHYSICAL_LOCATION_T A
                             inner join u_cell_rela_t B
                                on A.PHYSICAL_NAME = B.PHYSICAL_NAME  
                               and B.STATUS='正常' ";
        if (Dep_A.SelectedValue != "0")
        {
            sqlStr += " and B.CELL_CHNAME like '" + Dep_A.SelectedValue + "%'";
        }
        if (Cell_CHName_A.Text.Trim().Length > 0)
        {
            sqlStr += " and B.CELL_CHNAME like '%" + Tools.ReplaceString(Cell_CHName_A.Text.Trim(), 50) + "%'";
        }
        sqlStr += @" where A.Status = '正常' and cellowner = '联通' and not exists(select 1 from U_METER_PAY_T D 
                                        where A.PHYSICAL_NAME=D.PHYSICAL_NAME and D.Status='正在申请')
                                     group by A.PHYSICAL_NAME,A.CELLPOWER,A.METERWAY,A.BILLING_WAY,A.AMMETERNO ";
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
    }

    protected void QueryLocation_Click(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void Location_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Pay_Meter")
        {
            Location_Bind(e);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "open"
            , "$(function() {$('#MeterDiv').dialog('open');})", true);
        }
        else if (e.CommandName == "Change")
        {
            //TODO: To Change
            Change_Meter(e.CommandArgument.ToString());
            SetPager();
            JQueryAlert("该基站换表数据已更改！");
        }
    }

    private void Change_Meter(string argName)
    {
        string sqlStr = @"UPDATE U_METER_BASIC_T SET Meter=0,PEAK_METER=0,VALLEY_METER=0
             WHERE PHYSICAL_NAME=:PHYSICAL_NAME";
        OracleParameter[] _params = {
                    new OracleParameter(":PHYSICAL_NAME",OracleType.NVarChar,128)
                };
        _params[0].Value = argName;
        OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
    }

    private void Location_Bind(GridViewCommandEventArgs e)
    {
        string sqlStr = @"select t1.physical_name,airmodel,cellpower,meterway,billing_way,t3.meter_now as Last_Meter,t3.peak_meter AS Last_Peak
                                       ,t3.valley_meter AS Last_Valley,t3.pay_date as Last_Pay,t3.read_date as Last_Read,t3.order_no
                                       ,t3.usual_price,t3.peak_price,t3.valley_price,t3.REMAIN_METER,t3.Pay_Date
                                  from U_PHYSICAL_LOCATION_T T1
                                  left join (select physical_name,max(order_no) as maxNO from U_Meter_Pay_T 
                                 where status = '审核通过' 
                                 group by physical_name) T2
                                    on T1.PHYSICAL_NAME = T2.physical_name
                                  left join u_meter_pay_t T3
                                    on t1.physical_name = t3.physical_name
                                   and t3.order_no = t2.maxNO
                                 where t1.cellowner = '联通'
                                   and t1.status = '正常'
                                   and t1.physical_name=:PHYSICAL_NAME";
        OracleParameter[] _params = {
            new OracleParameter(":PHYSICAL_NAME",OracleType.NVarChar,128)
        };
        _params[0].Value = e.CommandArgument.ToString();
        DataRow dr = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params).Tables[0].Rows[0];
        PHYSICAL_NAME.Text = dr["physical_name"].ToString();
        Billing_Way.Text = dr["billing_way"].ToString();
        AIR_POWER.Text = dr["airmodel"].ToString();
        METERWAY.Text = dr["meterway"].ToString();
        if (dr["order_no"].ToString().Length > 0)
        {
            Meter_Last.ReadOnly = true;
            PEAK_METER_Last.ReadOnly = true;
            VALLEY_METER_Last.ReadOnly = true;
            Last_Date.ReadOnly = true;
            if (dr["REMAIN_METER"].ToString().Length > 0)
            {
                Last_Remain.ReadOnly = true;
                Last_Remain.Text = dr["REMAIN_METER"].ToString();
            }
            else
            {
                Last_Remain.ReadOnly = false;
            }
            Last_Pay.ReadOnly = true;
            Meter_Last.Text = dr["Last_Meter"].ToString();
            PEAK_METER_Last.Text = dr["Last_Peak"].ToString();
            VALLEY_METER_Last.Text = dr["Last_Valley"].ToString();
            Last_Date.Text = DateTime.Parse(dr["Last_Read"].ToString()).ToShortDateString();            
            USUAL_PRICE.Text = dr["usual_price"].ToString();
            PEAK_PRICE.Text = dr["peak_price"].ToString();
            VALLEY_PRICE.Text = dr["valley_price"].ToString();            
            if (dr["meterway"].ToString().Contains("协定"))
            {
                Last_Pay.Text = DateTime.Parse(dr["Last_Pay"].ToString()).ToShortDateString();                
            }
        }
        else
        {
            Meter_Last.ReadOnly = false;
            PEAK_METER_Last.ReadOnly = false;
            VALLEY_METER_Last.ReadOnly = false;
            Last_Date.ReadOnly = false;
            Last_Remain.ReadOnly = false;
            Last_Pay.ReadOnly = false;
        }
    }

    protected void LocationPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void UnAppPager_PageChanged(object sender, EventArgs e)
    {
        SetUnPager();
    }

    protected void BtnPayFee_Click(object sender, EventArgs e)
    {
        string sqlStr = @"insert into U_METER_PAY_T(ORDER_NO,PHYSICAL_NAME, METER_NOW, PEAK_METER
                                    , VALLEY_METER, USUAL_PRICE, PEAK_PRICE, VALLEY_PRICE, REMAIN_METER
                                    , PAY_DATE, READ_DATE, AGREE_FEE, LOSS_METER
                                    , IFAIR, RATING_FEE, REALPAY, FEE_REMARK, STATUS, INPUT_DATE, INPUTER
                                    , LAST_METER, LAST_PEAK, LAST_VALLEY, LAST_READ,Last_Remain,Last_Pay,Organ,VAT,FARE)
                                    values('YWDF'||LPAD(Meter_OrderNO_seq.Nextval,6,'0'),:PHYSICAL_NAME, :METER_NOW, :PEAK_METER
                                    , :VALLEY_METER, :USUAL_PRICE, :PEAK_PRICE, :VALLEY_PRICE, :REMAIN_METER
                                    , :PAY_DATE, :READ_DATE, :AGREE_FEE, :LOSS_METER
                                    , :IFAIR, :RATING_FEE, :REALPAY, :FEE_REMARK, :STATUS, SYSDATE, :INPUTER
                                    , :LAST_METER, :LAST_PEAK, :LAST_VALLEY, :LAST_READ,:Last_Remain,:Last_Pay,:Organ,:VAT,:FARE)";
        OracleParameter[] _params = {
            new OracleParameter(":PHYSICAL_NAME",OracleType.NVarChar,128),
            new OracleParameter(":METER_NOW",OracleType.Number,20),
            new OracleParameter(":PEAK_METER",OracleType.Number,20),
            new OracleParameter(":VALLEY_METER",OracleType.Number,20),
            new OracleParameter(":USUAL_PRICE",OracleType.Number,20),
            new OracleParameter(":PEAK_PRICE",OracleType.Number,20),
            new OracleParameter(":VALLEY_PRICE",OracleType.Number,20),
            new OracleParameter(":REMAIN_METER",OracleType.Number,20),
            new OracleParameter(":PAY_DATE",OracleType.DateTime),
            new OracleParameter(":READ_DATE",OracleType.DateTime),
            new OracleParameter(":AGREE_FEE",OracleType.Number,20),            
            new OracleParameter(":LOSS_METER",OracleType.Number,20),
            new OracleParameter(":IFAIR",OracleType.NVarChar,2),
            new OracleParameter(":RATING_FEE",OracleType.Number,20),
            new OracleParameter(":REALPAY",OracleType.Number,20),            
            new OracleParameter(":FEE_REMARK",OracleType.NVarChar,256),            
            new OracleParameter(":STATUS",OracleType.NVarChar,4),
            new OracleParameter(":INPUTER",OracleType.VarChar,16),           
            new OracleParameter(":LAST_METER",OracleType.Number,20),
            new OracleParameter(":LAST_PEAK",OracleType.Number,20),
            new OracleParameter(":LAST_VALLEY",OracleType.Number,20),
            new OracleParameter(":LAST_READ",OracleType.DateTime),
            new OracleParameter(":Last_Remain",OracleType.Number,20),
            new OracleParameter(":Last_Pay",OracleType.DateTime),
            new OracleParameter(":Organ",OracleType.NVarChar,4),
            new OracleParameter(":VAT",OracleType.Number,14),
            new OracleParameter(":FARE",OracleType.Number,14)
        };
        _params[0].Value = PHYSICAL_NAME.Text.Trim();        
        if (Meter_Now.Text.Trim().Length > 0)
        {
            _params[1].Value = Decimal.Parse(Meter_Now.Text.Trim());
        }
        else
        {
            _params[1].Value = DBNull.Value;
        }
        if (PEAK_METER.Text.Trim().Length > 0)
        {
            _params[2].Value = Decimal.Parse(PEAK_METER.Text.Trim());
        }
        else
        {
            _params[2].Value = DBNull.Value;
        }
        if (VALLEY_METER.Text.Trim().Length > 0)
        {
            _params[3].Value = Decimal.Parse(VALLEY_METER.Text.Trim());
        }
        else
        {
            _params[3].Value = DBNull.Value;
        }
        if (USUAL_PRICE.Text.Trim().Length > 0)
        {
            _params[4].Value = Decimal.Parse(USUAL_PRICE.Text.Trim());
        }
        else
        {
            _params[4].Value = DBNull.Value;
        }
        if (PEAK_PRICE.Text.Trim().Length > 0)
        {
            _params[5].Value = Decimal.Parse(PEAK_PRICE.Text.Trim());
        }
        else
        {
            _params[5].Value = DBNull.Value;
        }
        if (VALLEY_PRICE.Text.Trim().Length > 0)
        {
            _params[6].Value = Decimal.Parse(VALLEY_PRICE.Text.Trim());
        }
        else
        {
            _params[6].Value = DBNull.Value;
        }
        if (METERWAY.Text.Contains("预付费"))
        {
            //TODO:预付费&&复费率
            _params[7].Value = Decimal.Parse(REMAIN_METER.Text.Trim());
            _params[22].Value = Decimal.Parse(Last_Remain.Text.Trim());
        }
        else
        {
            _params[7].Value = DBNull.Value;
            _params[22].Value = DBNull.Value;
        }
        _params[8].Value = DateTime.Parse(Pay_Date.Text.Trim());        
        if (Agree_Fee.Text.Length > 0)
        {
            _params[10].Value = Decimal.Parse(Agree_Fee.Text.Trim());
        }
        else
        {
            _params[10].Value = DBNull.Value;
        }
        if (Loss_Meter.Text.Length > 0)
        {
            _params[11].Value = Decimal.Parse(Loss_Meter.Text.Trim());
        }
        else
        {
            _params[11].Value = DBNull.Value;
        }        
        _params[14].Value = Decimal.Parse(RealPay.Text.Trim());                 
        _params[15].Value = Fee_Remark.Text;
        _params[16].Value = "正在申请";        
        _params[17].Value = LoginUser;
        if (Meter_Last.Text.Trim().Length > 0)
        {
            _params[18].Value = Decimal.Parse(Meter_Last.Text.Trim());
        }
        else
        {
            _params[18].Value = DBNull.Value;
        }
        if (PEAK_METER_Last.Text.Trim().Length > 0)
        {
            _params[19].Value = Decimal.Parse(PEAK_METER_Last.Text.Trim());
        }
        else
        {
            _params[19].Value = DBNull.Value;
        }
        if (VALLEY_METER_Last.Text.Trim().Length > 0)
        {
            _params[20].Value = Decimal.Parse(VALLEY_METER_Last.Text.Trim());
        }
        else
        {
            _params[20].Value = DBNull.Value;
        }        
        
        if (METERWAY.Text.Contains("协定"))
        {
            _params[23].Value = DateTime.Parse(Last_Pay.Text.Trim());
            _params[9].Value = DBNull.Value;
            _params[12].Value = DBNull.Value;
            _params[13].Value = DBNull.Value;
            _params[21].Value = DBNull.Value;
        }
        else
        {
            _params[23].Value = DBNull.Value;
            _params[9].Value = DateTime.Parse(Read_Date.Text.Trim());
            _params[12].Value = IFAir.SelectedValue;            
            _params[13].Value = Decimal.Parse(Request.Form["Rating_Fee"].ToString());
            _params[21].Value = DateTime.Parse(Request.Form["Last_Date"].ToString());
        }
        _params[24].Value = Dep_A.SelectedValue;
        if (VAT.Text.Trim().Length > 0)
        {
            _params[25].Value = Decimal.Parse(VAT.Text.Trim());
        }
        else
        {
            _params[25].Value = DBNull.Value;
        }
        if (FARE.Text.Trim().Length > 0)
        {
            _params[26].Value = Decimal.Parse(FARE.Text.Trim());
        }
        else
        {
            _params[26].Value = DBNull.Value;
        }
        int res = OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
        if (res > 0)
        {
            Reset_Meter();
            SetPager();
            SetUnPager();
            JQueryAlert("电费录入成功！");
        }
    }

    private void Reset_Meter()
    {
        PHYSICAL_NAME.Text = "";
        Meter_Now.Text = "";
        PEAK_METER.Text = "";
        VALLEY_METER.Text = "";
        Meter_Last.Text = "";
        PEAK_METER_Last.Text = "";
        VALLEY_METER_Last.Text = "";
        Melting.Text = "";
        Pay_Date.Text = "";
        Read_Date.Text = "";
        Last_Date.Text = "";
        Agree_Fee.Text = "";
        Loss_Meter.Text = "";
        IFAir.SelectedIndex = -1;
        Rating_Fee.Text = "";
        Fee_Remark.Text = "";
        USUAL_PRICE.Text = "";
        PEAK_PRICE.Text = "";
        VALLEY_PRICE.Text = "";
        AIR_POWER.Text = "";
        ORDER_NO.Text = "";
        METERWAY.Text = "";
        REMAIN_METER.Text = "";
        Last_Remain.Text = "";
        Last_Pay.Text = "";
        RealPay.Text = "";
        VAT.Text = "";
        FARE.Text = "";
    }
}
